/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ import com.google.common.base.Objects;
/*     */ import com.google.common.base.Objects.ToStringHelper;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.imap.ImapMessage;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import java.util.Comparator;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class ZimbraHit
/*     */ {
/*     */   protected final Mailbox mailbox;
/*     */   protected final ZimbraQueryResults results;
/*     */   protected final Object sortValue;
/*     */   protected String cachedName;
/*     */   protected ImapMessage cachedImapMessage;
/*  43 */   protected int cachedModseq = -1;
/*  44 */   protected int cachedParentId = 0;
/*     */   
/*     */   public ZimbraHit(ZimbraQueryResults results, Mailbox mbx, Object sort) {
/*  47 */     this.mailbox = mbx;
/*  48 */     this.results = results;
/*     */     
/*  50 */     switch (results.getSortBy().getKey()) {
/*     */     case NONE: 
/*  52 */       this.sortValue = "";
/*  53 */       break;
/*     */     case ID: 
/*  55 */       this.sortValue = toInteger(sort);
/*  56 */       break;
/*     */     case DATE: 
/*     */     case SIZE: 
/*  59 */       this.sortValue = toLong(sort);
/*  60 */       break;
/*     */     default: 
/*  62 */       assert ((sort instanceof String)) : sort;
/*  63 */       this.sortValue = sort;
/*     */     }
/*     */   }
/*     */   
/*     */   private Long toLong(Object value)
/*     */   {
/*  69 */     if ((value instanceof Long))
/*  70 */       return (Long)value;
/*  71 */     if ((value instanceof String)) {
/*     */       try {
/*  73 */         return new Long((String)value);
/*     */       } catch (NumberFormatException e) {
/*  75 */         throw new IllegalArgumentException(value.toString());
/*     */       }
/*     */     }
/*  78 */     throw new IllegalArgumentException(value.toString());
/*     */   }
/*     */   
/*     */   private Integer toInteger(Object value)
/*     */   {
/*  83 */     if ((value instanceof Integer))
/*  84 */       return (Integer)value;
/*  85 */     if ((value instanceof String)) {
/*     */       try {
/*  87 */         return new Integer((String)value);
/*     */       } catch (NumberFormatException e) {
/*  89 */         throw new IllegalArgumentException(value.toString());
/*     */       }
/*     */     }
/*  92 */     throw new IllegalArgumentException(value.toString());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public abstract int getItemId()
/*     */     throws ServiceException;
/*     */   
/*     */ 
/*     */ 
/*     */   abstract int getConversationId()
/*     */     throws ServiceException;
/*     */   
/*     */ 
/*     */ 
/*     */   public abstract MailItem getMailItem()
/*     */     throws ServiceException;
/*     */   
/*     */ 
/*     */ 
/*     */   abstract void setItem(MailItem paramMailItem)
/*     */     throws ServiceException;
/*     */   
/*     */ 
/*     */ 
/*     */   abstract boolean itemIsLoaded()
/*     */     throws ServiceException;
/*     */   
/*     */ 
/*     */ 
/*     */   abstract String getName()
/*     */     throws ServiceException;
/*     */   
/*     */ 
/*     */ 
/*     */   public String getAcctIdStr()
/*     */   {
/* 129 */     return this.mailbox != null ? this.mailbox.getAccountId() : "NULL_ACCOUNTID";
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/*     */     try {
/* 135 */       return Objects.toStringHelper(this).add("mbox", this.mailbox.getId()).add("item", getItemId()).add("name", getName()).toString();
/*     */ 
/*     */     }
/*     */     catch (ServiceException e)
/*     */     {
/*     */ 
/* 141 */       return e.toString();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public Object getSortField(SortBy sort)
/*     */     throws ServiceException
/*     */   {
/* 151 */     switch (sort.getKey()) {
/*     */     case NONE: 
/* 153 */       return "";
/*     */     case SUBJECT: 
/*     */     case NAME: 
/*     */     case RCPT: 
/* 157 */       return ((String)this.sortValue).toUpperCase();
/*     */     }
/*     */     
/*     */     
/* 161 */     return this.sortValue;
/*     */   }
/*     */   
/*     */   public ItemId getParsedItemID() throws ServiceException
/*     */   {
/* 166 */     return new ItemId(this.mailbox, getItemId());
/*     */   }
/*     */   
/*     */   final Mailbox getMailbox() {
/* 170 */     return this.mailbox;
/*     */   }
/*     */   
/*     */   final ZimbraQueryResultsImpl getResults() {
/* 174 */     return (ZimbraQueryResultsImpl)this.results;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   int compareTo(SortBy sort, ZimbraHit other)
/*     */     throws ServiceException
/*     */   {
/* 187 */     switch (sort) {
/*     */     case DATE_ASC: 
/*     */     case SIZE_ASC: 
/* 190 */       return Long.signum(((Long)this.sortValue).longValue() - ((Long)other.sortValue).longValue());
/*     */     case DATE_DESC: 
/*     */     case SIZE_DESC: 
/* 193 */       return Long.signum(((Long)other.sortValue).longValue() - ((Long)this.sortValue).longValue());
/*     */     case SUBJ_ASC: 
/*     */     case NAME_ASC: 
/*     */     case NAME_LOCALIZED_ASC: 
/*     */     case RCPT_ASC: 
/* 198 */       return ((String)this.sortValue).toUpperCase().compareTo(((String)other.sortValue).toUpperCase());
/*     */     case SUBJ_DESC: 
/*     */     case NAME_DESC: 
/*     */     case NAME_LOCALIZED_DESC: 
/*     */     case RCPT_DESC: 
/* 203 */       return ((String)other.sortValue).toUpperCase().compareTo(((String)this.sortValue).toUpperCase());
/*     */     case ATTACHMENT_ASC: 
/*     */     case FLAG_ASC: 
/*     */     case PRIORITY_ASC: 
/* 207 */       return ((String)this.sortValue).compareTo((String)other.sortValue);
/*     */     case ATTACHMENT_DESC: 
/*     */     case FLAG_DESC: 
/*     */     case PRIORITY_DESC: 
/* 211 */       return ((String)other.sortValue).compareTo((String)this.sortValue);
/*     */     }
/*     */     
/* 214 */     throw new IllegalArgumentException(sort.name());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   boolean isLocal()
/*     */   {
/* 222 */     return true;
/*     */   }
/*     */   
/*     */   public ImapMessage getImapMessage() throws ServiceException {
/* 226 */     if (this.cachedImapMessage == null) {
/* 227 */       MailItem item = getMailItem();
/* 228 */       if (item == null) {
/* 229 */         return null;
/*     */       }
/* 231 */       this.cachedImapMessage = new ImapMessage(item);
/*     */     }
/* 233 */     return this.cachedImapMessage;
/*     */   }
/*     */   
/*     */   public int getModifiedSequence() throws ServiceException {
/* 237 */     if (this.cachedModseq < 0) {
/* 238 */       MailItem item = getMailItem();
/* 239 */       this.cachedModseq = (item != null ? item.getModifiedSequence() : 0);
/*     */     }
/* 241 */     return this.cachedModseq;
/*     */   }
/*     */   
/*     */   public int getParentId() throws ServiceException {
/* 245 */     if (this.cachedParentId == 0) {
/* 246 */       MailItem item = getMailItem();
/* 247 */       this.cachedParentId = (item != null ? item.getParentId() : -1);
/*     */     }
/* 249 */     return this.cachedParentId;
/*     */   }
/*     */   
/*     */   final void cacheImapMessage(ImapMessage value) {
/* 253 */     this.cachedImapMessage = value;
/*     */   }
/*     */   
/*     */   final void cacheModifiedSequence(int value) {
/* 257 */     this.cachedModseq = value;
/*     */   }
/*     */   
/*     */   final void cacheParentId(int value) {
/* 261 */     this.cachedParentId = value;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static Comparator<ZimbraHit> getSortAndIdComparator(SortBy sortOrder)
/*     */   {
/* 270 */     return new ZimbraHitSortAndIdComparator(sortOrder);
/*     */   }
/*     */   
/*     */   private static class ZimbraHitSortAndIdComparator implements Comparator<ZimbraHit> {
/*     */     private final SortBy sort;
/*     */     
/*     */     ZimbraHitSortAndIdComparator(SortBy sort) {
/* 277 */       this.sort = sort;
/*     */     }
/*     */     
/*     */     public int compare(ZimbraHit lhs, ZimbraHit rhs)
/*     */     {
/*     */       try {
/* 283 */         int result = lhs.compareTo(this.sort, rhs);
/* 284 */         int lhsId; int rhsId; if (result == 0) {
/* 285 */           lhsId = lhs.getItemId();
/* 286 */           if (lhsId <= 0) {
/* 287 */             lhsId = lhs.getConversationId();
/*     */           }
/* 289 */           rhsId = rhs.getItemId();
/* 290 */           if (rhsId <= 0) {
/* 291 */             rhsId = rhs.getConversationId();
/*     */           }
/*     */           
/* 294 */           if (this.sort.getDirection() == SortBy.Direction.ASC)
/* 295 */             result = lhsId - rhsId;
/*     */         }
/* 297 */         return rhsId - lhsId;
/*     */ 
/*     */       }
/*     */       catch (ServiceException e)
/*     */       {
/* 302 */         ZimbraLog.search.error("Failed to compare %s and %s", lhs, rhs, e); }
/* 303 */       return 0;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/ZimbraHit.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */